package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.domain.dto.bug.AddBugRequest;
import com.zh.domain.dto.bug.PageBugDynamicRequest;
import com.zh.domain.dto.bug.PageBugRequest;
import com.zh.domain.dto.bug.UpdateBugRequest;
import com.zh.domain.dto.task.AddTaskRequest;
import com.zh.domain.dto.task.PageTaskDynamicRequest;
import com.zh.domain.dto.task.PageTaskRequest;
import com.zh.domain.dto.task.UpdateTaskRequest;
import com.zh.domain.entity.BugDynamics;
import com.zh.domain.vo.bug.BugDynamicVo;
import com.zh.domain.vo.bug.BugVo;
import com.zh.domain.vo.task.TaskDynamicVo;
import com.zh.domain.vo.task.TaskVo;
import com.zh.exception.ThrowUtils;
import com.zh.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "BugController", value = "缺陷控制器")
@RestController
@RequestMapping("/bug")
@Slf4j
@SaCheckLogin
public class BugController {
    @Resource
    private BugService bugService;

    @PostMapping("/add")
    @ApiOperation(value = "addBug", notes = "添加缺陷")
    public BaseResponse<Boolean> addBug(@RequestBody AddBugRequest addBugRequest) {
        ThrowUtils.throwIf(addBugRequest == null, ErrorCode.PARAMS_ERROR);
        Boolean result = bugService.addBug(addBugRequest);
        return ResultUtils.success(result);
    }

    @PostMapping("/page")
    @ApiOperation(value = "pageBugs", notes = "分页查询缺陷")
    public BaseResponse<Page<BugVo>> pageBugs(@RequestBody PageBugRequest pageBugRequest) {
        ThrowUtils.throwIf(pageBugRequest == null, ErrorCode.PARAMS_ERROR);
        Page<BugVo> result = bugService.pageBugs(pageBugRequest);
        return ResultUtils.success(result);
    }
    @PutMapping("/update")
    @ApiOperation(value = "updateBug", notes = "更新缺陷")
    public BaseResponse<Boolean> updateBug(@RequestBody UpdateBugRequest updateBugRequest){
        ThrowUtils.throwIf(updateBugRequest == null, ErrorCode.PARAMS_ERROR);
        Boolean result = bugService.updateBug(updateBugRequest);
        return ResultUtils.success(result);
    }

    @GetMapping
    @ApiOperation(value = "/getBugDetail", notes = "获取缺陷详情")
    public BaseResponse<BugVo> getBugDetail(Integer id) {
        ThrowUtils.throwIf(null == id, ErrorCode.PARAMS_ERROR);
        BugVo bugVo = bugService.getDetail(id);
        return ResultUtils.success(bugVo);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "deleteBug", notes = "删除缺陷")
    public BaseResponse<Boolean> deleteBug(Integer bugId) {
        ThrowUtils.throwIf(null == bugId, ErrorCode.PARAMS_ERROR);
        Boolean result = bugService.removeById(bugId);
        return ResultUtils.success(result);
    }
    @PostMapping("/dynamics/page")
    @ApiOperation(value = "pageBugDynamics",notes = "分页获取缺陷动态")
    public BaseResponse<Page<BugDynamicVo>> pageBugDynamics(@RequestBody PageBugDynamicRequest pageBugDynamicRequest){
        ThrowUtils.throwIf(null == pageBugDynamicRequest,ErrorCode.PARAMS_ERROR);
        Page<BugDynamicVo> resultPage = bugService.pageDynamics(pageBugDynamicRequest);
        return ResultUtils.success(resultPage);
    }

    @PostMapping("/listUndoBugList")
    @ApiOperation(value = "listUndoBugList", notes = "查询我的未完成缺陷列表")
    public BaseResponse<List<BugVo>> listUndoBugList() {
        List<BugVo> result = bugService.listUndoBugList();
        return ResultUtils.success(result);
    }
}
